﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.Markup;

namespace UIT.Controls.Data.Parameters
{
    public class Parameter : IParameter, IBindingContainer
    {
        #region IParameter Members

        public string Name { get; set; }

        public Binding ValueBinding { get; set; }

        private object value;

        public object Value
        {
            get
            {
                if (ValueBinding != null)
                {
                    ValueBinding.EnsureInit(Parent.Parent, typeof(IParameter).GetProperty("Value"));
                    value = ValueBinding.GetValue();
                }
                return value;
            }
            set { this.value = value; }
        }


        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            Value = null;
        }

        #endregion

        #region IContainable<IDataProvider> Members

        public IDataProvider Parent
        {
            get;
            set;
        }

        #endregion


        public event EventHandler ParameterChanged;

        public Markup.Binding GetBinding(string propertyName)
        {
            if (propertyName == "Value" || propertyName == "ValueBinding")
                return ValueBinding;
            return null;
        }

        public void SetBinding(string propertyName, Markup.Binding binding)
        {
            if (propertyName == "Value" || propertyName == "ValueBinding")
            {
                ValueBinding = binding;

                if (ParameterChanged != null)
                    ParameterChanged(this, EventArgs.Empty);
            }
        }

        public bool RemoveBinding(string propertyName)
        {
            if (propertyName == "Value" || propertyName == "ValueBinding")
            {
                if (ValueBinding != null)
                {
                    ValueBinding = null;
                    return true;
                }
                return false;
            }
            return false;
        }
    }
}
